package co.edu.javeriana.aes.patrones.monopolio.cliente;

import co.edu.javeriana.aes.patrones.monopolio.cliente.chat.ChatJPanel;
import co.edu.javeriana.aes.patrones.monopolio.util.exception.MonopolyException;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;

/**
 *
 * @author gersua
 */
public class InitFrame extends JFrame implements WindowListener {

    private ViewHandler viewHandler;
    private MonopolyClient monopolyClient;
    /**************************************************************************/
    private LoginJPanel loginJPanel;
    private ShowRooms showRooms;
    private ChatJPanel chatJPanel;
    private GamePanel gamePanel;
    private WaitPlayers waitPlayers;

    public InitFrame() throws MonopolyException {
        viewHandler = new ViewHandler();
        try {
            monopolyClient = new MonopolyClient();
        } catch (Exception ex) {
            throw new MonopolyException("Fatal error! It was imposible create Monopoly Client.", ex);
        }

        loginJPanel = new LoginJPanel(monopolyClient, viewHandler);
        showRooms = new ShowRooms(monopolyClient, viewHandler);
        chatJPanel = new ChatJPanel(monopolyClient, viewHandler);
        gamePanel = new GamePanel(monopolyClient, viewHandler);
        waitPlayers = new WaitPlayers(monopolyClient, viewHandler);
        gamePanel = new GamePanel(monopolyClient, viewHandler);

        initComponents();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(getMaximumSize());

        /**********************************************************************/
        JInternalFrame loginInternalFrame = new MyInternarFrame("", true, false, false, true);
        loginInternalFrame.setSize(viewHandler.getDimension());
        loginInternalFrame.setVisible(true);
        loginInternalFrame.add(loginJPanel);
        /**********************************************************************/
        JInternalFrame showRoomInternalFrame = new MyInternarFrame("", true, false, false, true);
        showRoomInternalFrame.setSize(viewHandler.getDimension());
        showRoomInternalFrame.setVisible(false);
        showRoomInternalFrame.add(showRooms);
        showRoomInternalFrame.addComponentListener(showRooms);
        /**********************************************************************/
        JInternalFrame chatInternalFrame = new MyInternarFrame("", true, false, false, true);
        chatInternalFrame.setSize(viewHandler.getDimension());
        chatInternalFrame.setVisible(false);
        chatInternalFrame.add(chatJPanel);
        /**********************************************************************/
//        JInternalFrame gameInternalFrame = new MyInternarFrame("", true, false, false, true);
//        gameInternalFrame.setSize(750, 650);
//        gameInternalFrame.setVisible(false);
//        gameInternalFrame.add(gamePanel);
//        /**********************************************************************/
        JInternalFrame waitPlayerInternalFrame = new MyInternarFrame("", true, false, false, true);
        waitPlayerInternalFrame.setSize(viewHandler.getDimension());
        waitPlayerInternalFrame.setVisible(false);
        waitPlayerInternalFrame.add(waitPlayers);
        /**********************************************************************/
        JInternalFrame gamePanelInternalFrame = new MyInternarFrame("", true, false, false, true);
        gamePanelInternalFrame.setSize(viewHandler.getDimension());
        gamePanelInternalFrame.setVisible(false);
        gamePanelInternalFrame.add(gamePanel);
        /**********************************************************************/
        viewHandler.setLoginInternalFrame(loginInternalFrame);
        viewHandler.setShowRoomInternalFrame(showRoomInternalFrame);
        viewHandler.setChatInternalFrame(chatInternalFrame);
        viewHandler.setWaitPlayersInternalFrame(waitPlayerInternalFrame);
        viewHandler.setGamePanelInternalFrame(gamePanelInternalFrame);

        desktop.add(loginInternalFrame);
        desktop.add(showRoomInternalFrame);
        desktop.add(chatInternalFrame);
        desktop.add(gamePanelInternalFrame);
        desktop.add(waitPlayerInternalFrame);
    }

    public LoginJPanel getLoginJPanel() {
        return loginJPanel;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        desktop = new javax.swing.JDesktopPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(desktop, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 1086, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(desktop, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 475, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JDesktopPane desktop;
    // End of variables declaration//GEN-END:variables

    @Override
    public void windowOpened(WindowEvent we) {
    }

    @Override
    public void windowClosing(WindowEvent we) {
        monopolyClient.close();
    }

    @Override
    public void windowClosed(WindowEvent we) {
    }

    @Override
    public void windowIconified(WindowEvent we) {
    }

    @Override
    public void windowDeiconified(WindowEvent we) {
    }

    @Override
    public void windowActivated(WindowEvent we) {
    }

    @Override
    public void windowDeactivated(WindowEvent we) {
    }
}
